package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.saas.domain.entity.ContractPerformanceEntity;
import com.xbongbong.saas.enums.subform.ContractPerformanceSubFormEnum;
import com.xbongbong.paas.help.subform.abstracts.AbstractTransferSubForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 合同业绩 es和数据库格式转化
 * @author 魏荣杰
 * @date 2020/8/25 10:27
 * @since
 * @version
 */
@Component
public class ContractPerformanceTransferSubForm extends AbstractTransferSubForm {
    private static final Logger LOG = LoggerFactory.getLogger(ContractPerformanceTransferSubForm.class);

    public PaasFormDataEntity transferEntityToFormData(ContractPerformanceEntity contractPerformanceEntity) {
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        paasFormDataEntity.setUpdateTime(contractPerformanceEntity.getUpdateTime());
        paasFormDataEntity.setAddTime(contractPerformanceEntity.getAddTime());
        paasFormDataEntity.setId(contractPerformanceEntity.getId());
        paasFormDataEntity.setCorpid(contractPerformanceEntity.getCorpid());
        paasFormDataEntity.setDel(contractPerformanceEntity.getDel());

        JSONObject dbJson = JSONObject.parseObject(JSON.toJSONString(contractPerformanceEntity));

        JSONObject data = new JSONObject();

        ContractPerformanceSubFormEnum[] values = ContractPerformanceSubFormEnum.values();
        for (ContractPerformanceSubFormEnum value : values) {
            String dbAttr = value.getDbAttr();
            String dataAttr = value.getDataAttr();
            Integer fieldType = value.getFieldType();
            formatDataForEs(dbJson, data, dbAttr, dataAttr, fieldType);
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }



    public List<PaasFormDataEntity> transferEntityToFormDataList(List<ContractPerformanceEntity> contractPerformanceEntities) {
        List<PaasFormDataEntity> paasFormDataEntities = new ArrayList<>();
        for (ContractPerformanceEntity contractPerformanceEntity : contractPerformanceEntities) {
            PaasFormDataEntity paasFormDataEntity = transferEntityToFormData(contractPerformanceEntity);
            paasFormDataEntities.add(paasFormDataEntity);
        }
        return paasFormDataEntities;
    }



    public ContractPerformanceEntity transferFormDataToEntity(PaasFormDataEntity paasFormDataEntity) {

        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();
        ContractPerformanceSubFormEnum[] contractPerformanceSubFormEnums = ContractPerformanceSubFormEnum.values();
        for (ContractPerformanceSubFormEnum contractPerformanceSubFormEnum : contractPerformanceSubFormEnums) {
            String dbAttr = contractPerformanceSubFormEnum.getDbAttr();
            String dataAttr = contractPerformanceSubFormEnum.getDataAttr();
            Integer fieldType = contractPerformanceSubFormEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);

        }
        ContractPerformanceEntity contractPerformanceEntity = JSON.parseObject(dbData.toJSONString(), ContractPerformanceEntity.class);
        contractPerformanceEntity.setAddTime(paasFormDataEntity.getAddTime());
        contractPerformanceEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        contractPerformanceEntity.setCorpid(paasFormDataEntity.getCorpid());
        contractPerformanceEntity.setDel(paasFormDataEntity.getDel());
        contractPerformanceEntity.setId(paasFormDataEntity.getId());

        return contractPerformanceEntity;
    }


    public List<ContractPerformanceEntity> transferFormDataToEntityList(List<PaasFormDataEntity> paasFormDataEntities) {
        List<ContractPerformanceEntity> contractPerformanceEntities = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            ContractPerformanceEntity contractPerformanceEntity = transferFormDataToEntity(paasFormDataEntity);
            contractPerformanceEntities.add(contractPerformanceEntity);
        }

        return contractPerformanceEntities;
    }
}
